package logic

import (
	"api/pkg"
	"context"
	"user_srv/user"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type HandlerUserLoginLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewHandlerUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HandlerUserLoginLogic {
	return &HandlerUserLoginLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *HandlerUserLoginLogic) HandlerUserLogin(req *types.RequestUserLogin) (resp *types.Response, err error) {
	//TODO: 登录
	login, err := l.svcCtx.UserSrv.UserLogin(l.ctx, &user.UserLoginRequest{
		Mobile:   req.Mobile,
		Password: req.Password,
		SmsCode:  req.SmsCode,
	})
	if err != nil {
		return &types.Response{
			Message: "短信输入有误",
			Code:    "500",
			Data:    nil,
		}, nil
	}
	handler, _ := pkg.TokenHandler(string(login.Id))
	return &types.Response{
		Message: "登录成功",
		Code:    "200",
		Data: map[string]interface{}{
			"login":   login,
			"handler": handler,
		},
	}, nil
}
